package com.ruoyi.web.controller.chat;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.robot.domain.InterfaceSetting;
import com.ruoyi.robot.service.IChatRobotSettingService;
import com.ruoyi.robot.service.IInterfaceSettingService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.tool.SendMsg;
import javafx.scene.control.Tab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/chat/chatting")
public class ChatController extends BaseController {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IInterfaceSettingService interfaceSettingService;

    //查询用户角色
    @GetMapping("/getUserRole/{userId}")
    public TableDataInfo getLoginUserRole(@PathVariable(value = "userId",required = true) String id)
    {
        try
        {
            //需要限定一个用户对应一个角色
            List<Long> roleList = sysRoleService.selectRoleListByUserId(Long.valueOf(id));
            return getDataTable(roleList);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return getDataTable(new ArrayList<Long>());
        }
    }

    //查询用户头像
    @PostMapping("/getUserAvatar")
    public TableDataInfo getUsersAvatar(@RequestBody JSONObject userIdList)
    {
        ArrayList<HashMap<String,String>> mapList = new ArrayList<>();
        HashMap<String,String> avatarMap = new HashMap<>();
        try
        {
            for (String id : userIdList.keySet())
            {
                SysUser user = sysUserService.selectUserById(userIdList.getLong(id));
                if(user != null)
                {
                    if(user.getAvatar() != null && user.getAvatar().length() > 0)
                    {
                        avatarMap.put(id,user.getAvatar());
                    }
                    else
                    {
                        avatarMap.put(id,"");
                    }
                }
            }
            mapList.add(avatarMap);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return getDataTable(new ArrayList<HashMap<String,String>>());
        }

        return getDataTable(mapList);
    }



}
